package test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.BeforeClass;
import org.junit.Test;

import po.Customer;
/**
 * PO层中与持久层有关系
 * VO层中与表现出有关系
 * @author DELL
 *
 */
public class demo01 {
	private static EntityManagerFactory factory;
	@SuppressWarnings("unused")
	private EntityManager entityManager;
	
	@BeforeClass
	public static void beforClass() {
		factory = Persistence.createEntityManagerFactory("JPADemo01-Lesson");
	}
	
	@Test
	public void createTable() {
		/**
		 * 普通的创建表的操作
		 */
		//持久化单元的实例化
		EntityManager entityManager = factory.createEntityManager();
	}
	
	@Test
	public void testSave01() {
		//必须进行事务提交
		Customer customer = new Customer(1,"John",100);
		//通过实体管理类工厂中获取到实体管理对象
		EntityManager entityManager = factory.createEntityManager();
		//事务对象的创建
		EntityTransaction transaction = entityManager.getTransaction();
		//开启事务
		transaction.begin();
		try {
			//执行插入操作
			entityManager.persist(customer);
			//提交事务
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			//这里抛出的异常就是说明了是数据表的操作异常
			transaction.rollback();
		} finally {
			//关闭管理对象
			entityManager.close();
		}
	}
	
	@Test
	public void testFind01() {
		EntityManager entityManager = factory.createEntityManager();
		Customer customer = entityManager.find(Customer.class, 1);
		System.out.println("====testFind01===="+customer.getCname());
	}
	
	@Test
	public void testUpdate01() {
		EntityManager entityManager = factory.createEntityManager();
		Customer customer = entityManager.find(Customer.class, 1);
		System.out.println("====testUpdate01===="+customer.getCname());
		
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		customer.setCname("Tom");
		transaction.commit();
		
		entityManager.close();
	}
	
	@Test
	public void testDelete01() {
		EntityManager entityManager = factory.createEntityManager();
		Customer customer = entityManager.find(Customer.class, 1);
		System.out.println("====testDelete01===="+customer.getCname());
		
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
		entityManager.remove(customer);
		transaction.commit();
		entityManager.close();
	}
	
	@Test
	public void testSaveImage() {
		Customer customer = new Customer(2,"Steve",100);
		EntityManager entityManager;
		EntityTransaction transaction;
		InputStream input;
		try {
			input = new FileInputStream("I:/1.jpg");
			byte[] buffer = new byte[input.available()];
			input.read();
			customer.setImage(buffer);
			entityManager = factory.createEntityManager();
			//事务对象的创建
			transaction = entityManager.getTransaction();
			//开启事务
			transaction.begin();
			//执行插入操作
			entityManager.persist(customer);
			//提交事务
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testFindImage() {
		EntityManager entityManager = factory.createEntityManager();
		Customer customer = entityManager.find(Customer.class, 2);
		System.out.println("====testFind02Image===="+customer.getCname());
		
		try {
			byte[] buffer = customer.getImage();
			OutputStream outputStream = new FileOutputStream("I:/2.jpg");
			outputStream.write(buffer);
			outputStream.close();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	@Test
	public void testSaveBirthday() {
		/**
		 * 保存日期类型的数据
		 */
		Customer customer = new Customer(2,"Steve",100);
		EntityManager entityManager;
		EntityTransaction transaction;
		InputStream input;
		try {
			input = new FileInputStream("I:/1.jpg");
			byte[] buffer = new byte[input.available()];
			input.read();
			customer.setImage(buffer);
			customer.setSex("M");
			//保存日期的示范
			customer.setBithday(new Date());
			entityManager = factory.createEntityManager();
			//事务对象的创建
			transaction = entityManager.getTransaction();
			//开启事务
			transaction.begin();
			//执行插入操作
			entityManager.persist(customer);
			//提交事务
			transaction.commit();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
